package persistence.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import persistence.bean.Configuration;
import persistence.bean.MappedStatement;
import persistence.io.Resource;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class XmlConfigBuilder {
    private Configuration configuration;

    public XmlConfigBuilder() {
        configuration = new Configuration();
    }

    /***
     * 解析配置文件封装 Configuration
     * @param inputStream
     * @return
     */
    public Configuration parseConfig(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        // 解析 sqlMapConfig.xml
        List<Element> nodes = rootElement.selectNodes("//property");
        Properties properties = new Properties();
        for (Element node : nodes) {
            String name = node.attributeValue("name");
            String value = node.attributeValue("value");
            properties.setProperty(name, value);
        }
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setJdbcUrl(properties.getProperty("url"));
        hikariConfig.setDriverClassName(properties.getProperty("driverClass"));
        hikariConfig.setUsername(properties.getProperty("user"));
        hikariConfig.setPassword(properties.getProperty("password"));
        HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);
        configuration.setDataSource(hikariDataSource);
        // 解析 mapper.xml
        List<Element> mappers = rootElement.selectNodes("//mapper");
        for (Element mapper : mappers) {
            String mapperPath = mapper.attributeValue("resource");
            InputStream mapperInputStream = Resource.getResourceAsInputStream(mapperPath);
            parseMapper(mapperInputStream);
        }
        return configuration;
    }

    // 封装mapper
    private void parseMapper(InputStream mapperInputStream) throws DocumentException {
        Document document = new SAXReader().read(mapperInputStream);
        Element rootElement = document.getRootElement();

        List<Element> nodes = new ArrayList<>();
        nodes.addAll(rootElement.selectNodes("//select"));
        nodes.addAll(rootElement.selectNodes("//delete"));
        nodes.addAll(rootElement.selectNodes("//update"));
        nodes.addAll(rootElement.selectNodes("//insert"));
        String namespace = rootElement.attributeValue("namespace");
        for (Element node : nodes) {
            String id = node.attributeValue("id");
            String resultType = node.attributeValue("resultType");
            String parameterType = node.attributeValue("parameterType");
            String sql = node.getTextTrim();
            MappedStatement statement = new MappedStatement();
            statement.setId(id);
            statement.setParameterType(parameterType);
            statement.setSql(sql);
            statement.setResultType(resultType);
            String key = namespace + "." + id;
            configuration.getMappedStatementMap().put(key, statement);
        }
    }
}
